package cn.felord.verse.configure;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.kie.dmn.feel.lang.types.impl.ComparablePeriod;
import org.kie.kogito.config.ConfigBean;
import org.kie.kogito.dmn.rest.DMNFEELComparablePeriodSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * jackson配置
 */
@Configuration(proxyBeanMethods = false)
public class JacksonConfiguration {

    /**
     * Jackson2 object mapper builder customizer
     *
     * @return the jackson 2 object mapper builder customizer
     */
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer(ConfigBean configBean) {
        return jacksonObjectMapperBuilder -> {
            if (!configBean.failOnEmptyBean()) {
                jacksonObjectMapperBuilder.featuresToDisable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
            }
            jacksonObjectMapperBuilder.modules(
                            new Jdk8Module(),
                            new JavaTimeModule(),
                            new SimpleModule()
                    )
                    .serializerByType(ComparablePeriod.class, new DMNFEELComparablePeriodSerializer())
//                    .dateFormat(new StdDateFormat().withColonInTimeZone(true).withTimeZone(TimeZone.getDefault()))
                    .serializationInclusion(JsonInclude.Include.NON_NULL);
        };
    }


/*   new ObjectMapper() .registerModule(new JavaTimeModule())
            .registerModule((new SimpleModule())
            .addSerializer(ComparablePeriod .class, new DMNFEELComparablePeriodSerializer()))
            .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
    .disable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS);*/

}
